package com.template.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.template.context.UserContext;
import com.template.dto.Comment;
import com.template.mapper.CommentMapper;
import com.template.result.Result;
import com.template.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public Result addComment(Comment comment) throws JsonProcessingException {
        LocalDateTime dateTime=LocalDateTime.now();

        List<Integer> likeList=new ArrayList<>();

        System.out.println(comment);
//        ObjectMapper objectMapper=new ObjectMapper();
        String likeMan=objectMapper.writeValueAsString(likeList);

        comment.setLikeMan(likeMan);
        comment.setCreateTime(dateTime);
        comment.setGood(0);
        commentMapper.addComment(comment);
        return Result.success();
    }

    @Override
    public List<Comment> getCommentsByTimeOrGood(Map<String, Object> params) {
        List<Comment> commentList=commentMapper.getCommentByTimeOrGood(params);


        Integer currentUserId = (Integer) params.get("userId");

        commentList.forEach(comment -> {
            try {
                // 将likeMan JSON字符串转换为List<Integer>
                List<Integer> likeManList = objectMapper.readValue(
                        comment.getLikeMan(),
                        new TypeReference<List<Integer>>() {}
                );


                // 检查当前用户是否在这个列表里
                if (likeManList != null && likeManList.contains(currentUserId)) {
                    comment.setIsgood(true);
                } else {
                    comment.setIsgood(false);
                }
            } catch (Exception e) {
                // 处理JSON解析异常
                e.printStackTrace();
                System.out.println("点赞异常");
                comment.setIsgood(false);
            }
        });

        // 调用 Mapper 中的方法进行查询
        return commentList;
    }

    @Override
    public void noGood(Integer userId, Integer id) throws JsonProcessingException {
        Comment comment=commentMapper.getComment(id);

        List<Integer> likeManList =objectMapper.readValue(comment.getLikeMan(), new TypeReference<List<Integer>>() {});

        likeManList.remove(UserContext.getCurrentUserId());

        String likeMan=objectMapper.writeValueAsString(likeManList);
        comment.setLikeMan(likeMan);


        comment.setGood(comment.getGood()-1);

        commentMapper.updateComment(comment);

    }

    @Override
    public void good(Integer userId,Integer id) throws JsonProcessingException {
        Comment comment=commentMapper.getComment(id);

        List<Integer> likeManList =objectMapper.readValue(comment.getLikeMan(), new TypeReference<List<Integer>>() {});
        
        likeManList.add(userId);

        String likeMan=objectMapper.writeValueAsString(likeManList);
        comment.setLikeMan(likeMan);

//        Integer goodNum= comment.getGood();
        comment.setGood(comment.getGood()+1);

        commentMapper.updateComment(comment);
    }

}
